package fsdownloadserver;
import FsInterfaces.IFileServer;
import FsInterfaces.ServerBandwidthBean;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
 * @author bo
 */
public class RmiCaller implements Runnable{
    
    ServerInfo serverInfo;
    ICallBack callBack;
    String aTitle;

    public RmiCaller(ServerInfo serverInfo, ICallBack callBack, String aTitle){
        this.serverInfo = serverInfo;
        this.callBack = callBack;
        this.aTitle = aTitle;
    }

    public void run(){
        System.out.print("Contacting "+serverInfo.getUrl()+":"+serverInfo.getPort()+":"+serverInfo.getName()+ " ... ");
        try {
            Registry registry=LocateRegistry.getRegistry(serverInfo.getUrl(), serverInfo.getPort());
            IFileServer fs = (IFileServer)(registry.lookup(serverInfo.getName()));
            int bandWidth = fs.CheckFile(aTitle);
            System.out.println(" found! Bandwidth availale: "+bandWidth+" Kb/s");
            callBack.setBandWidthCallback(new ServerBandwidthBean(serverInfo.getUrl(), bandWidth));
        }
        catch (Exception e) {
            System.out.println(" Server not responding...");
            callBack.setCrashed();
        }
    }
}
